<?php

require_once ROOT . 'server/classes/arquitetura/dao/AbstractDao.class.php';

class UsuarioDao extends AbstractDao {

    const TABLE_NAME = 'usuario';

    private $entity;
    private $nome;
    private $sobrenome;
    private $sexo;
    private $email;
    private $dtNasc;
    private $senha;
    private $rand;
    private $ativo;

    /**
     * Construtor padrao.
     */
    public function __construct($entity) {
        $this->setEntity($entity);
    }

    public function setEntity($entity) {
        $this->entity = $entity;
    }

    public function getEntity() {
        return $this->entity;
    }

    /**
     * Prepare variables to be used in sql command.
     */
    private function prepareVariables() {
        $this->nome = Util::isNotEmpty($this->entity->getNome()) ? "'" . $this->entity->getNome() . "'" : 'NULL';
        $this->sobrenome = Util::isNotEmpty($this->entity->getSobrenome()) ? "'" . $this->entity->getSobrenome() . "'" : 'NULL';
        $this->sexo = Util::isNotEmpty($this->entity->getSexo()) ? "'" . $this->entity->getSexo() . "'" : 'NULL';
        $this->email = Util::isNotEmpty($this->entity->getEmail()) ? "'" . $this->entity->getEmail() . "'" : 'NULL';
        $this->dtNasc = Util::isNotEmpty($this->entity->getDtNasc()) ? "'" . $this->entity->getDtNasc() . "'" : 'NULL';
        $this->senha = Util::isNotEmpty($this->entity->getSenha()) ? "'" . $this->entity->getSenha() . "'" : 'NULL';
        $this->rand = Util::isNotEmpty($this->entity->getRand()) ? trim($this->entity->getRand()) : 'NULL';
        $this->ativo = Util::isNotEmpty($this->entity->getAtivo()) ? "'" . $this->entity->getAtivo() . "'" : 'NULL';
    }

    /**
     * Execute the insert command on the table: usuario.
     * @return int Number of affected rows.
     */
    public function getInsertSQL() {
        $this->prepareVariables();
        $sql = 'INSERT INTO ' . self::TABLE_NAME . ' (nome, sobrenome, sexo, email, dt_nasc, senha, rand, ativo) VALUES (' . $this->nome . ', ' . $this->sobrenome . ', ' . $this->sexo . ', ' . $this->email . ', ' . $this->dtNasc . ', ' . $this->senha . ', ' . $this->rand . ', ' . $this->ativo . ')';
        return $sql;
    }

    /**
     * Execute the update command on the table usuario.
     * @return int Number of affected rows.
     */
    public function getUpdateSQL() {
        $this->prepareVariables();
        $sql = 'UPDATE ' . self::TABLE_NAME . ' SET nome = ' . $this->nome . ', sobrenome = ' . $this->sobrenome . ', sexo = ' . $this->sexo . ', email = ' . $this->email . ', dt_nasc = ' . $this->dtNasc . ', senha = ' . $this->senha . ', rand = ' . $this->rand . ', ativo = ' . $this->ativo . ', update_date = now() WHERE ID = ' . $this->entity->getId();
        return $sql;
    }

    /**
     * Execute the delete command on the table usuario.
     * @return int Number of affected rows.
     */
    public function getDeleteSQL() {
        if ($this->entity->getId()) {
            $sql = 'DELETE FROM ' . self::TABLE_NAME . ' WHERE ID = ' . $this->entity->getId();
            return $sql;
        } else {
            //Nothing to remove.
            return 0;
        }
    }

    /**
     * Execute the select command on the table usuario. To retrive rows.
     * @param String $where
     * @return Array of rows.
     */
    public function getFindSQL($where = null) {
        $sql = 'SELECT id, nome, sobrenome, sexo, email, dt_nasc, senha, rand, ativo, update_date FROM ' . self::TABLE_NAME;
        if ($where) {
            $sql .= ' where ' . $where;
        }
        return $sql;
    }

    /**
     * Execute the select command on the table usuario. To retrive rows no null values
     * @param String $where
     * @return Array of rows.
     */
    public function getFindNoNullSQL($where = null) {
        $sql = 'SELECT IFNULL(id, \'\') id, IFNULL(nome, \'\') nome, IFNULL(sobrenome, \'\') sobrenome, IFNULL(sexo, \'\') sexo, IFNULL(email, \'\') email, IFNULL(dt_nasc, \'\') dt_nasc, IFNULL(senha, \'\') senha, IFNULL(rand, \'\') rand, IFNULL(ativo, \'\') ativo, IFNULL(update_date, \'\') update_date FROM ' . self::TABLE_NAME;
        if ($where) {
            $sql .= ' where ' . $where;
        }
        return $sql;
    }

    /**
     * Execute the select command on the table usuario. To retrive rows no null values Using entitys values. 
     */
    public function getByEntityFindNoNullSQL() {
        $where = '1 = 1';
        $where .= Util::isNotEmpty($entity->getNome()) ? ' AND nome = \'' . trim($entity->getNome()) . '\'' : '';
        $where .= Util::isNotEmpty($entity->getSobrenome()) ? ' AND sobrenome = \'' . trim($entity->getSobrenome()) . '\'' : '';
        $where .= Util::isNotEmpty($entity->getSexo()) ? ' AND sexo = \'' . trim($entity->getSexo()) . '\'' : '';
        $where .= Util::isNotEmpty($entity->getEmail()) ? ' AND email = \'' . trim($entity->getEmail()) . '\'' : '';
        $where .= Util::isNotEmpty($entity->getDtNasc()) ? ' AND dt_nasc = \'' . trim($entity->getDtNasc()) . '\'' : '';
        $where .= Util::isNotEmpty($entity->getSenha()) ? ' AND senha = \'' . trim($entity->getSenha()) . '\'' : '';
        $where .= Util::isNotEmpty($entity->getRand()) ? ' AND rand = \'' . trim($entity->getRand()) . '\'' : '';
        $where .= Util::isNotEmpty($entity->getAtivo()) ? ' AND ativo = \'' . trim($entity->getAtivo()) . '\'' : '';
        $where .= Util::isNotEmpty($entity->getUpdateDate()) ? ' AND update_date = \'' . trim($entity->getUpdateDate()) . '\'' : '';
        return $this->getFindNoNullSQL($where);
    }

}

?>
